package com.erp.module.zhichengJianyan

import android.content.Context
import android.util.Log
import com.erp.Interface.OnUserDataInterface
import com.erp.bean.JieHaoKBean
import com.erp.bean.ProcessInspectionJieHaoBean
import com.erp.module.jiehao.JieHaoContract
import com.erp.pull.BasePullP
import com.erp.util.JsonUtil
import com.erp.util.Myutil
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import java.lang.Exception
import java.util.ArrayList

class IProcessInspection(var list :MutableList<ProcessInspectionJieHaoBean>, var V : ProcessInspectionContract.InspectionJieHaoView, var context: Context,var sousuobean : InspectionSouSuobean) : BasePullP(), ProcessInspectionContract.InspectionJieHaoP {

    var M = ProcessInspectionModel()
    init {
        PullV = V
    }
    override fun InspectionJieHao(batch: String, inspection: String) {
        V.Inspectionjiehao()
        M.postInspection(context,batch,inspection,object : OnUserDataInterface{
            override fun onUserData(data: String?) {
                if(data == null){
                    V.InspectionJieHaoNo("")
                    return
                }
                Myutil.MyTost("修改成功")
                V.InspectionJieHaoOk(data)
            }

            override fun onNot(throwable: String?) {
                if(throwable == null){
                    V.InspectionJieHaoNo("")
                    return
                }
                V.InspectionJieHaoNo(throwable)
                Myutil.MyTost("修改失败")
            }

        })
    }

    override fun getData(page: Int, pagesize: Int, onUserDataInterface: OnUserDataInterface) {
        M.getInspection(context,page,pagesize,sousuobean,onUserDataInterface)
    }

    override fun jsondata(data: String?) {
        Log.d("TAAAG","data="+data)
        try{
            val js = JSONArray(data)
            for(i in 0 until js.length()){
                val jo=js[i] as JSONObject
                var bean= ProcessInspectionJieHaoBean()
                bean.inspection_id = JsonUtil.getvolue(jo,"inspection_id")
                bean.mb_id= JsonUtil.getvolue(jo,"mb_id")
                bean.batch= JsonUtil.getvolue(jo,"batch")
                bean.ctime= JsonUtil.getvolue(jo,"ctime")
                bean.inspection= JsonUtil.getvolue(jo,"inspection")
                bean.plan_warehousing_time= JsonUtil.getvolue(jo,"plan_warehousing_time")
                bean.process_inspection_name= JsonUtil.getvolue(jo,"process_inspection_name")
                bean.process_inspection_time= JsonUtil.getvolue(jo,"process_inspection_time")
                list.add(bean)
                if(bean.process_inspection_time == ""){
                    bean.inspection = "0"
                }else{
                    bean.inspection = "1"
                }
            }
        }catch (e : JSONException){
            Log.d("TAAAG","JSONException="+e.toString())
            e.printStackTrace()
        }
    }

    override fun listClean() {
        list.clear()
    }

    override fun sousuo(suosuo: InspectionSouSuobean) {
        this.sousuobean = suosuo
    }

    override fun getWuLiao_DaiMa() {
    var zhiliangid: MutableList<String> = ArrayList()
    var zhiliangname: MutableList<String> = ArrayList()
    M.getWuLiao_LeiBie_DaiMa(context,object : OnUserDataInterface{
        override fun onUserData(data: String?) {
            val jo = JSONObject(data)
            val keys = jo.keys()
            while (keys.hasNext()) {
                val key = keys.next()
                zhiliangid.add(key)
                zhiliangname.add(JsonUtil.getvolue(jo, key))
            }
            V.getWuLiao_DaiMaOK(zhiliangid,zhiliangname)
        }

        override fun onNot(throwable: String?) {
            V.getWuLiao_DaiMaNO()
        }

    })
}

}